<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-14 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{		
		switch($this->act)
		{
			case 'list':
				$this->getList();
				break;
			case 'info':
				$this->getInfo();
				break;
			case 'comment_list':
				$this->getCommentList();
				break;
			case 'add_comment':
				$this->addComment();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}
	
	private function getList()
	{
		//DBFunction::getInstance()->setDebug();

		$this->request['is_show'] = '1';
		
		$result = get_complaint_list($this->request);
		
		if(!check_array($result)) $this->message('cp_03', STATUS_F);
		
		foreach($result as &$temp)
		{
			$temp['status'] = ($temp['status'] != '') ? $temp['status'] : '未处理';
			
			$temp['business_logo'] = ($temp['business_logo'] != '') ? BASE_URL.$temp['business_logo'] : '';
			$temp['business_logo_s'] = str_replace('_o.', '_s.', $temp['business_logo']);
			
			$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($result);
		$this->response['list'] = $result;
	}
	
	private function getInfo()
	{
		//DBFunction::getInstance()->setDebug();
		
		$cid = isset($this->request['cid']) ? $this->request['cid'] : '';
		
		$result = get_complaint(array('cid'=>$cid));
		
		if(!check_array($result)) $this->message('cp_03', STATUS_F);
		
		$deal_list = get_complaint_deal_list(array('cid'=>$cid));
		if(check_array($deal_list))
		{
			foreach($deal_list as &$temp)
			{
				$temp['status'] = ($temp['status'] != '') ? $temp['status'] : '未处理';
			}
		}
		
		$result['deal_list'] = $deal_list;
		
		$temp_result = get_complaint_comment_list(array('cid'=>$cid));
		$result['comment_count'] = $temp_result['count'];

		$result['status'] = ($result['status'] != '') ? $result['status'] : '未处理';
		
		$result['business_logo'] = ($result['business_logo'] != '') ? BASE_URL.$result['business_logo'] : '';
		$result['business_logo_s'] = str_replace('_o.', '_s.', $result['business_logo']);
		
		$result['image'] = ($result['image'] != '') ? BASE_URL.$result['image'] : '';
		$result['image_s'] = str_replace('_o.', '_s.', $result['image']);
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $result;
	}
	
	private function getCommentList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$cid = isset($this->request['cid']) ? $this->request['cid'] : '';
		
		$result = get_complaint(array('cid'=>$cid));
		
		if(!check_array($result)) $this->message('cp_03', STATUS_F);
		
		$result = get_complaint_comment_list(array('cid'=>$cid, 'limit'=>$this->request['limit'], 'offset'=>$this->request['offset']));
		$list = $result['result'];
		
		if(!check_array($list)) $this->message('cm_01', STATUS_F);
		
		foreach($list as &$temp)
		{
			$temp['create_date'] = date('Y-m-d', $temp['create_date']);
		}
				
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
	}
	
	private function addComment()
	{
		//DBFunction::getInstance()->setDebug();
		
		$cid = isset($this->request['cid']) ? (int)$this->request['cid'] : '';
		$uid = isset($this->request['uid']) ? (int)$this->request['uid'] : '';
		$desc = isset($this->request['desc']) ? $this->request['desc'] : '';
		
		$result = get_complaint(array('cid'=>$cid));
		
		if(!check_array($result)) $this->message('cp_03', STATUS_F);
		
		$result = add_complaint_comment(array('cid'=>$cid, 'uid'=>$uid, 'desc'=>$desc));
		
		if($result)
		{
			update_complaint_favorite_count(array('id'=>$cid));
			
			$this->message('g_06');
		}
		else
		{
			$this->message('g_07', STATUS_F);
		}
	}
}